package cn.jdy.plugins.wap;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.exception.BaseException;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.Member;

/**
 * 会员注册控制类
 *
 * @author yangyi
 */
@Controller
@RequestMapping("/register")
public class RegisterCtrl extends BaseCtrl {
  /**
   * 注册初始化
   */
  @RequestMapping("")
  public ModelAndView register(){
    ModelAndView mv = new ModelAndView(super.t + "/register");
    return mv;
  }
  
  /**
   * 用户注册页面
   */
  @RequestMapping("/registerIndex")
  public ModelAndView registerIndex() {
    ModelAndView mv = new ModelAndView("/register.jsp");
    return mv;
  }
  /**
	 * @return /web/register/forSaveMember?username=test&password=123456
	 * @title : 保存注册会员信息
	 * @description: 保存注册信息 跳转到首页或前一个页面
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/forSaveMember1")
	public ModelAndView forSaveRegisterMember1(Member memberBo,String authcode) {
		ModelAndView mv = new ModelAndView();
//		if(null == request.getSession().getAttribute(GlobalConstant._PHONECODE)||!authcode.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))){
//		  mv.setViewName("/register.jsp");
//    mv.addObject("msg", "短信验证码无效！");
//    return mv;
//		}
		  
		try {
			memberBo.setUserType("0");// 个人注册
			memberBo = memberService.saveMember(memberBo);
//			memberService.updateMoney(memberBo.getId(), GlobalConstant.SMONEY_REGISTER);
			session.setAttribute("user", memberBo);// 设置登录状态
			mv.addObject("msg", "success");
//			mv.setViewName("/login.jsp");
			mv.setViewName("/registerOk.jsp");
		} catch(BaseException e) {
			mv.setViewName("/register.jsp");
			mv.addObject("msg", "注册失败！");
			BaseLog.e(this.getClass(), "保存会员注册信息错误", e);
		}
		return mv;
	}
	/**
	 * @title : 保存注册会员信息
	 * @description: 保存注册信息 跳转到首页或前一个页面
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/forSaveMember")
	public ModelAndView forSaveRegisterMember(Member memberBo,String authcode) {
		ModelAndView mv = new ModelAndView();
		if(null == request.getSession().getAttribute(GlobalConstant._PHONECODE)||!authcode.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))){
		  mv.setViewName(super.t +"/register.jsp");
      mv.addObject("msg", "短信验证码无效！");
      return mv;
		}
 
		try {
			memberBo.setUserType("0");// 个人注册
			memberBo = memberService.saveMember(memberBo);
			memberService.updateMoney(memberBo.getId(), GlobalConstant.SMONEY_REGISTER);//添加积分
			session.setAttribute("user", memberBo);// 设置登录状态
			mv.addObject("msg", "success");
			mv.setViewName("redirect:/");// 跳转到首页
		} catch(BaseException e) {
		  mv.setViewName(super.t +"/register.jsp");
			mv.addObject("msg", "注册失败！");
			BaseLog.e(this.getClass(), "保存会员注册信息错误", e);
		}
		return mv;
	}


	/**
	 * @title  校验用户名是否被占用
	 * @author  wuzhiliang
	 * @date  2014-03-13 16:53
	 */
	@RequestMapping("/checkUnickusername")
	@ResponseBody
	public Map<String, Object> checkUnickusername(String param) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(memberService.findcheckUsername(param) > 0) {
			jsonMap.put("status", "n");
			jsonMap.put("info", "该用户已注册，请更换一个");
		} else {
			jsonMap.put("status", "y");
		}
		return jsonMap;
	}

	/**
	 * @return
	 * @title
	 * @description: 校验手机号是否被占用
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkUnickmobile")
	@ResponseBody
	public Map<String, Object> checkUnickmobile(String param) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(memberService.findcheckUsermobile(param) > 0) {
			jsonMap.put("status", "n");
			jsonMap.put("info", "该手机号已被注册");
		} else {
			jsonMap.put("status", "y");
		}
		return jsonMap;
	}

	/**
	 * @return
	 * @title
	 * @description: 校验短信验证码
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkAuthcode")
	@ResponseBody
	public Map<String, Object> checkAuthcode(String param) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(param!=null && param.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))) {
			jsonMap.put("status", "y");
		} else {
			jsonMap.put("status", "n");
			jsonMap.put("info", "验证码错误");
		}
		return jsonMap;
	}

	/**
	 * @return
	 * @title
	 * @description: 校验验证码
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkVerifyCode")
	@ResponseBody
	public Map<String, Object> checkVerifyCode(String authcode) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(authcode.equals(request.getSession().getAttribute("verifyCode"))) {
			jsonMap.put("success", "0");
		} else {
			jsonMap.put("success", "1");
		}
		return jsonMap;
	}

	/**
	 * @return
	 * @title
	 * @description: AIAX方式校验短信验证码
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkAuthcodeString")
	@ResponseBody
	public String checkAuthcodeString(String authcode) {
		if(authcode.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))) {
			return "true";
		}
		return "false";
	}

}
